<?php
namespace app\index\controller;
use think\Controller;
use think\Db;
use think\View;
use think\Cookie;
use app\index\model\IndexModel;
class Biao extends Controller
{

	public function _initialize()
	{
		parent::_initialize();

	}

	public function index()
	{

	}

	public function daybiao()
	{
		$view = new View();
		$m=new IndexModel;
 		$uid=Cookie::get("user_id");
		$powers=$m->getPowersByUid($uid);
		$view->assign('powers',$powers);
		
		$store_id = cookie::get('store_id');
		if (empty($store_id)) {
			$this->error('店铺信息错误', 'Index/index');
			exit;
		}
		$psdate=strtotime(date("Y-m-d"));
		$d1=strtotime(date("Y-m-d"))-21600;
		if (!empty($_POST['ps_date'])) {
			$psdate=strtotime($_POST['ps_date']);
			$d1 = $psdate-21600;
			
		}
		$d2=$d1+86400;
		//店铺名称
		$whstore = array(
				'store_id' => $store_id,
				'status' => 1
		);
		$store = Db::name('merchant_store')->where($whstore)->find();
	

		//今日下单总数
    			//按配送日期取出
    			$where3 = array(
    				'order_status'=>[['=',1],['=',3],['=',4],['=',5],['=',8],['=',9],'or'],
    				'ps_date'=>$psdate
    			);
    			$list = Db::name('waimai_order')->where($where3)->order('ps_time')->select();
    			$ps_list=array();
    			foreach($list as $k=>$s){
    				$ps_list[$s['order_id']]=$s;
    			}
    			//按下单日期取出
    			$wh = array(
    				'order_status'=>[['=',1],['=',3],['=',4],['=',5],['=',8],['=',9],'or'],
    				'create_time'=>[['>=',$d1],['<',$d2],'and'] 
    			);
    			$clist = Db::name('waimai_order')->where($wh)->select();
    			foreach($clist as $k=>$s){
    				if(empty($ps_list[$s['order_id']])){
    					$ps_list[$s['order_id']]=$s;
    				}
    				
    			}
	    		$allcount=count($ps_list);
	    
		//线上支付金额
		
		$online_pay=Db::name('waimai_order')->field('sum(online_pay)')->where($wh)->find();
		//线下支付金额
		$allpay=Db::name('waimai_order')->field('sum(discount_price)')->where($wh)->find();
		$offline_pay=(double)$allpay['sum(discount_price)']-(double)$online_pay['sum(online_pay)'];
		$view->assign('online_pay',$online_pay['sum(online_pay)']);
		$view->assign('offline_pay',$offline_pay);
		//总销售额
		$allsum = Db::name('waimai_order')->field('sum(discount_price)')->where($wh)->find();
		

		//今日关注公众号数
		
//		$whuser = array(
//				'add_time' =>[['>=',$d1],['<=',$d2],'and']
//		);
		//$nucount2 = Db::name('user')->field('count(uid)')->where($whuser)->find();    //	公众号新关注人数
		//$nucount = Db::name('user')->field('count(uid)')->find();  //	公众号关注总人数
		
		$t1=$d1;
		$t2=$t1+3600;
		if (!empty($_POST['ps_date'])) {
			$t1 = strtotime($_POST['ps_date'])-21600;
			$t2 = $t1+3600;
		}
		$stepsale['0']=0;
		$d=18;
		$b=0;
		//按24小时获取下单数量
		for($i=1;$i<=24;$i+=1){	
			if($d==25){//时间到24点就变成1
				$d=1;
			}
			
			$wht=array(
				'create_time'=>[['>=',$t1],['<',$t2],'and'],
				'order_status' => [['=',1],['=',3],['=',4],['=',5],['=',8],['=',9],'or']
			);
			$counts = Db::name('waimai_order')->where($wht)->count();
			$sarray[$d]=$counts;
			$newuser_array[$d]=0;
			$olduser_array[$d]=$counts;
			if($i==1){
				$stepsale[$d]=$counts;
			}else{
				$stepsale[$d]=$b+$counts;
			}
			$b+=$counts;
			$t1=$t2;
			$t2+=3600;
			$d+=1;
				
		}
		
		//查询今天卖的数量最多的前10个菜
		$goods_sell=array();
		$whsell=array(
			'create_time'=>[['>',$d1],['<=',$d2],'and'],
			'status'=>1
		);
		$sell_list=Db::name('waimai_sell_log')->where($whsell)->select();
		if(!empty($sell_list['0'])){
			foreach($sell_list as $k=>$s){
				if(empty($goods_sell[$s['goods_id']])){
					$goods_sell[$s['goods_id']]=$s['num'];
				}else{
					$goods_sell[$s['goods_id']]+=$s['num'];
				}
				
			}
		}
		arsort($goods_sell);
		$i=1;
		foreach($goods_sell as $k=>$s){
			if($i>10)break;
			$goods_name=Db::name('waimai_goods')->where('goods_id',$k)->find();
			if(!empty($goods_name)){
				$goods10[$k]['name']=$goods_name['name'];
			}else{
				$name=Db::name('waimai_sell_log')->where('goods_id',$k)->find();
				$goods10[$k]['name']=$name['name'];
			}
			$goods10[$k]['num']=$s;
			$i+=1;
		}
		$view->assign("goods10",$goods10);
	//按小时查询新用户个数
		$new_user = array();
		$ulist1 = Db::name('waimai_order')->field('order_id,uid,create_time,address')->where($wh)->select();
$uids=array();
		foreach ($ulist1 as $key => $vo) {
			$orders[$vo['uid']]=$vo['order_id'];
			$uids[$key]=$vo['uid'];
			$createtime[$vo['uid']]=$vo['create_time'];
			$uaddress[$vo['uid']]=$vo['address'];
		}

		$uids2=array_unique($uids);
		$u = 0;
		foreach ($uids2 as $key => $vo) {
			$whu = array(
					'uid' => $vo,
					'create_time'=>['<',$d2],
					'order_status' => [['=',1],['=',3],['=',4],['=',5],['=',8],['=',9],'or']
			);
			$ucount = Db::name('waimai_order')->where($whu)->count();
			if ($ucount == 1) {
				$newname = Db::name('user')->field('uid,nickname,add_time')->where('uid',$vo)->find();
				++$u;            //新增客户+1
				$h=date("H",$createtime[$vo]);
				$t=$h;
				if($h=="01"){
					$t = substr($h, 1); 
				}elseif($h=="02"){
					
					$t = substr($h, 1); 
				}elseif($h=="03"){
					$t = substr($h, 1); 
				}elseif($h=="04"){
					$t = substr($h, 1); 
				}elseif($h=="05"){
					$t = substr($h, 1); 
				}elseif($h=="06"){
					$t = substr($h, 1); 
				}elseif($h=="07"){
					$t = substr($h, 1); 
				}elseif($h=="08"){
					$t = substr($h, 1); 
				}elseif($h=="09"){
					$t = substr($h, 1); 
				}

			$newuser = unserialize($uaddress[$vo]);
			$new_user[$u]['address']=$newuser['address'].$newuser['detail'];
			$new_user[$u]['order_id']=$orders[$vo];
			$new_user[$u]['nickname']=$newname['nickname'];
			$new_user[$u]['add_time']=$newname['add_time'];
			$new_user[$u]['uid']=$vo;
					$addr_zhen=explode("虎门镇",$newuser['address']);
					if(count($addr_zhen)>1){
						$new_user[$u]['address']=$addr_zhen[count($addr_zhen)-1].$newuser['detail'];
					}else{
						$addr_shi=explode("东莞市",$newuser['address']);
						if(count($addr_shi)>1){
							$new_user[$u]['address']=$addr_shi[count($addr_shi)-1].$newuser['detail'];
						}
					}
			
				$newuser_array[$t]=$newuser_array[$t]+1;
				$olduser_array[$t]=$sarray[$t]-$newuser_array[$t];
				
			}
			
			
		}

$view->assign('stepsale', $stepsale);    //每小时下单量
$view->assign('olduser_array', $olduser_array);    //每小时下单量
$view->assign('sarray', $sarray);    //每小时下单量
$view->assign('newuser_array', $newuser_array);    //每小时新用户个数
		$view->assign('time1', $d1);    //日期
		$view->assign('time2', $d2);    //日期
		$view->assign('store', $store);    //店铺
		$view->assign('allcount', $allcount);    //总单量
		$view->assign('new_user', $new_user);    //新增客户
		$view->assign('u', $u);    //新增客户数量
		$view->assign('allsum', $allsum);    //总销售额
	//	$view->assign('nucount', $nucount);    //公众号关注总人数
	//	$view->assign('nucount2', $nucount2);    //公众号今日关注人数

		
	return $view->fetch();
	}

	public function yuebiao()
	{
		$view = new View();
		$m=new IndexModel;
 		$uid=Cookie::get("user_id");
		$powers=$m->getPowersByUid($uid);
		$view->assign('powers',$powers);
		
		$store_id = cookie::get('store_id');
		if (empty($store_id)) {
			$this->error('店铺信息错误', 'Index/index');
			exit;
		}
		$month = date("Y/m");
		if (!empty($_POST['ps_date'])) {
			$month = $_POST['ps_date'];
		}
		
			$marray = explode('/', $month);
			$year=$marray['0'];
			$month=$marray['1'];
			$t=$year . "-".$month."-01";
			$days =date('t', strtotime($t));
			
			$day1 = $marray['0'] . "-" . $marray['1'] . "-01";
			$day2 = $marray['0'] . "-" . $marray['1'] . "-". $days;

			$day1str = strtotime($day1);        //生成时间戳
			$day2str = strtotime($day2);

		//店铺名称
		$whstore = array(
				'store_id' => $store_id,
				'status' => 1
		);
		$store = Db::name('merchant_store')->where($whstore)->find();
		$mstart=$day1str-21600;
		$mend=$day2str+68400;
		$wh = array(
				'create_time' => [['<', $mend], ['>=', $mstart], 'and'],
				'order_status'=>[['=',1],['=',3],['=',4],['=',5],['=',8],['=',9],'or']
		);
		$daystart=$day1str-21600;
		$dayend=$daystart+86400;
$psd1=$day1str;
		for($i=1;$i<=$days;$i+=1){
			$daysale[$i]=0;
			$day_newsale[$i]=0;
			$day_oldsale[$i]=0;
			$alldays[$i]=$i;
		}
		for($i=1;$i<=$days;$i+=1){
				$whday = array(
					'create_time' => [['>=', $daystart], ['<', $dayend], 'and'],
					'order_status'=>[['=',1],['=',3],['=',4],['=',5],['=',8],['=',9],'or']
				);
				
				//按日期查询单数
					$list=array();
					$time1 = $psd1;
					$whpsd = array(
						'ps_date'=>$time1,
						'order_status'=>[['=',1],['=',3],['=',4],['=',5],['=',8],['=',9],'or']
					);
					$order_list= Db::name("waimai_order")->where($whpsd)->order('ps_time')->select();

					
					foreach($order_list as $key=>$vo){
						$list[$vo['order_id']]=$vo;
					}
					//按下单日期取出
					$c1=$time1-21600;
					$c2=$c1+86400;
					$whc = array(
							'create_time'=>[['>=',$c1],['<',$c2],'and'],
							'order_status'=>[['=',1],['=',3],['=',4],['=',5],['=',8],['=',9],'or']
					);
					$clist = Db::name("waimai_order")->where($whc)->order('ps_time')->select();
					foreach($clist as $key=>$vo){
						if(empty($list[$vo['order_id']])){
							$list[$vo['order_id']]=$vo;
						}	
					}
				
			$daysale[$i] = count($list);
			
			$day_oldsale[$i]=$daysale[$i];
			//按小时查询新用户个数
		$new_user = array();
		$ulist = Db::name('waimai_order')->field('uid,create_time,address')->where($whday)->select();
				foreach ($ulist as $key => $vo) {
					$whu = array(
							'uid' => $vo['uid'],
							'create_time'=>['<',$dayend],
							'order_status' => [['=',1],['=',3],['=',4],['=',5],['=',8],['=',9],'or']
					);
					$ucount = Db::name('waimai_order')->where($whu)->count();
					if ($ucount == 1) {
						           //新增客户+1
						$day_newsale[$i]=$day_newsale[$i]+1;
						$day_oldsale[$i]=$daysale[$i]-$day_newsale[$i];	
					}
				}
				
			$daystart=$dayend;
			$dayend=$dayend+86400;	
			$psd1=$psd1+86400;
		}
		

		//该月总销量
		$allcount = Db::name('waimai_order')->where($wh)->count();
		//该月总销售额
		$allsum = Db::name('waimai_order')->field('sum(discount_price)')->where($wh)->find();
		//该月总销售额
		$paytypes = Db::name('waimai_order')->field('pay_type')->where($wh)->select();
		$pay_type['offline']=0;
		$pay_type['weixin']=0;
		foreach($paytypes as $p){
			if($p['pay_type']=="weixin"){
				$pay_type['weixin']+=1;
			}else{
				$pay_type['offline']+=1;
			}
		}
		//查询该月新用户个数
		$new_user = array();
		$ulist = Db::name('waimai_order')->field('uid,address')->where($wh)->select();

		$u = 0;
		$sex['man']=0;
		$sex['woman']=0;
		foreach ($ulist as $key => $vo) {        //检查有多少新用户
			$whu = array(
					'uid' => $vo['uid'],
					'create_time'=>['<',$dayend],
					'order_status' => [['=',1],['=',3],['=',4],['=',5],['=',8],['=',9],'or']
			);
			$ucount = Db::name('waimai_order')->where($whu)->count();
			$usex = Db::name('user')->field('sex')->where('uid',$vo['uid'])->find();
			if ($ucount == 1) {
				++$u;            //新增客户+1
				if(!empty($vo['address'])){
					$newuser = unserialize($vo['address']);
					$new_user[$u]=$newuser['address'];
					$addr_zhen=explode("虎门镇",$newuser['address']);
					if(count($addr_zhen)>1){
						$new_user[$u]=$addr_zhen[count($addr_zhen)-1];
					}else{
						$addr_shi=explode("东莞市",$newuser['address']);
						if(count($addr_shi)>1){
							$new_user[$u]=$addr_shi[count($addr_shi)-1];
						}
					}
				}
			}
			if($usex['sex']==1){
				$sex['man']+=1;
			}else{
				$sex['woman']+=1;
			}
		}
		
		//查询该月卖的数量最多的前20个菜
		$goods_sell=array();
		$goods10=array();
		$whsell=array(
			'create_time'=>[['<', $day2str], ['>=', $mstart], 'and'],
		);
		$sell_list=Db::name('waimai_sell_log')->where($whsell)->select();
		if(!empty($sell_list['0'])){
			foreach($sell_list as $k=>$s){
				if(empty($goods_sell[$s['goods_id']])){
					$goods_sell[$s['goods_id']]=$s['num'];
				}else{
					$goods_sell[$s['goods_id']]+=$s['num'];
				}
				
			}
		}
		arsort($goods_sell);
		$i=1;
		foreach($goods_sell as $k=>$s){
			if($i>17)break;
			$goods_name=Db::name('waimai_goods')->where('goods_id',$k)->find();
			if(!empty($goods_name)){
				$goods10[$k]['name']=$goods_name['name'];
			}else{
				$name=Db::name('waimai_sell_log')->where('goods_id',$k)->find();
				$goods10[$k]['name']=$name['name'];
			}
			$goods10[$k]['num']=$s;
			$i+=1;
		}
		$view->assign("goods10",$goods10);
$view->assign('alldays', $alldays);    //一个月天数
$view->assign('daysale', $daysale);    //每天销量
$view->assign('day_newsale', $day_newsale);    //每天新用户下单人数
$view->assign('day_oldsale', $day_oldsale);    //
$view->assign('pay_type', $pay_type);    //支付方式
$view->assign('sex', $sex);    //男女数量
		$view->assign('day1str', $day1str);    //月初
		$view->assign('day2str', $day2str);    //月末
		$view->assign('store', $store);    //店铺
		$view->assign('yallcount', $allcount);    //总单量
		$view->assign('ynew_user', $new_user);    //新增客户
		$view->assign('yu', $u);    //新增客户数量
		$view->assign('yallsum', $allsum);    //总销售额

		return $view->fetch();
}
public function nianbiao(){
	$view = new View();
	$m=new IndexModel;
 		$uid=Cookie::get("user_id");
		$powers=$m->getPowersByUid($uid);
		$view->assign('powers',$powers);
		
		$store_id = cookie::get('store_id');
		if (empty($store_id)) {
			$this->error('店铺信息错误', 'Index/index');
			exit;
		}


		//按配送时间段获取数量
		$monthstr = ""; //月份字符串
		$countstr = "";//销量字符串
		$salestr = "";//销售额字符串
		$year = 2016;
		$month = 5;
		$day = 01;
			
		
		$startday = (int)($year . "0" . $month);//开始月份
		$nowday = date("Ym");//结束月份
		$j = $nowday - $startday+1;
		for ($i = 0; $i < $j; $i += 1) {
			$t=$year . "-".$month."-01";
			$days =date('t', strtotime($t));
			$first = "";
			$last = "";
			if ($month < 10) {
				$first = $year . "-0" . $month . "-" . $day;
				$last = $year . "-0" . $month . "-" . $days;
			} else {
				$first = $year . "-" . $month . "-" . $day;
				$last = $year . "-" . $month . "-" . $days;
			}
			//查询每月销量
			$firstday = strtotime($first);
			$lastday = strtotime($last);
			$whday = array(
					'create_time' => [['>=', $firstday], ['<=', $lastday], 'and'],
					'order_status'=> [['=',1],['=',3],['=',4],['=',5],['=',8],['=',9],'or']
			);
			//查询每月销量
			$mcount = Db::name('waimai_order')->where($whday)->count();
			//查询每月销售额
			$sales = Db::name('waimai_order')->field('sum(discount_price)')->where($whday)->find();
			if (empty($sales)) {
				$sale = 0;
			} else {
				$sale = $sales['sum(discount_price)'];
			}
			//将月份和销量组成字符串，方便作图
			if ($monthstr != "") {
				$monthstr = $monthstr . "," . $month;
				$countstr = $countstr . "," . $mcount;
				$salestr = $salestr . "," . $sale;
			} else {
				$monthstr = $month;
				$countstr = $mcount;
				$salestr = $sale;
			}
			//年月增加
			if ($month == 12) {
				$month = 1;
				++$year;
			} else {
				++$month;
			}

		}

		$montharray = explode(",", $monthstr);
		$countarray = explode(",", $countstr);
		$salearray = explode(",", $salestr);

		$carray = array();

		for ($i = 0; $i < count($montharray); $i += 1) {
			$carray[$i]['yue'] = $montharray[$i];
		}
		for ($i = 0; $i < count($countarray); $i += 1) {
			$carray[$i]['count'] = (int)$countarray[$i];
		}
		for ($i = 0; $i < count($salearray); $i += 1) {
			$carray[$i]['sale'] = (double)$salearray[$i];
		}


		$view->assign('monthstr', $monthstr);
		$view->assign('countstr', $countstr);
		$view->assign('salestr', $salestr);
		$view->assign('carray', $carray);    //销量数组


		//$view->assign('nucount',$nucount);	//公众号关注总人数
		//$view->assign('nucount2',$nucount2);	//公众号今日关注人数
		return $view->fetch();
	}

}
?>